home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / miscpas.zip / PRIMES.PAS < prev    next >
Pascal/Delphi Source File  |  1980-01-01  |  1KB  |  42 lines

  1. PROGRAM PRIMES; (*$R-*)
  2.  
  3. (* This program will calculate and display all prime numbers *)
  4. (* between 1 and 30000. The algorithm of the program is to   *)
  5. (* start out with an array containing representatives for    *)
  6. (* all odd numbers between 3 and 29999. Starting from 3 and  *)
  7. (* working upwards, each odd number is then tested. If a     *)
  8. (* number is still a member of the list when it is tested,   *)
  9. (* it is a prime number, and thus it is printed, and all odd *)
  10. (* multiples of the number are eliminated from the list. As  *)
  11. (* can be expected, the program is quite slow on calculating *)
  12. (* the very first primes, but from then on it gets faster    *)
  13. (* and faster. Note that 1 and 2 are assumed to be primes,   *)
  14. (* and not actually calculated.                     *)
  15.  
  16. CONST
  17.   MAX2 = 15000;   (* MAXPRIME/2 *)
  18.   MAX3 = 10000;   (* MAXPRIME/3 *)
  19.  
  20. VAR
  21.   I,J,K: INTEGER;
  22.   TEST: ARRAY[2..MAX2] OF BOOLEAN;
  23.  
  24. BEGIN
  25.   WRITE(1:8,2:8);
  26.   FOR I:=2 TO MAX2 DO TEST[I]:=TRUE;
  27.   FOR I:=2 TO MAX2 DO
  28.   IF TEST[I] THEN
  29.   BEGIN
  30.     J:=I+I-1; WRITE(J:8);
  31.     IF J<MAX3 THEN
  32.     BEGIN
  33.       K:=I+J;
  34.       WHILE K<=MAX2 DO
  35.       BEGIN
  36.     TEST[K]:=FALSE; K:=K+J;
  37.       END;
  38.     END;
  39.   END;
  40.   WRITELN;
  41. END.
  42.